<?php

class cats_ctrl extends Controller
{


	public function __construct()
	{
		parent::__construct();

	}

	//获取整理所有分类,用于展示
	public function get_cats(){
		if(!$this->ajax_err){
            //$parentID = $_REQUEST['parentID'];

            $rs_data = (object)null;

            $key = 'mac_front_getitemcats';
            ccDel($key);
            $rs_data->data = ccLoad($key);
            //如果缓存中存在数据,直接从缓存中读取数据
            if ($rs_data->data != false) {
                $rs_data->getmode = 'cache';
                //var_dump($result->items->item);
                return $this->ajax($rs_data);
            } else {
                $data = array();

                $_mdl = Loader::mdl('cats_mdl');
                $data = $_mdl->get_list();

                //分类收集
                //$data = o2a($rs_cats->picture_categories->picture_category);


                $all_cats = array();
                foreach ($data as $cat) {
                    $tmp_sub_cats = array();

                    $all_cats[$cat['cid']] = array(
                        'cid' => $cat['cid'],
                        'name' => $cat['name'],
                        'parent_cid' => $cat['parent_cid'],
                        'pic_url' => $cat['pic_url'],
                        'sub_cats' => $tmp_sub_cats,
                        'children_id' => 0,
                        'value' => array($cat['cid'])
                    );
                    if ($cat['parent_cid'] != 0) {
                        $all_cats[$cat['parent_cid']]['children_id'] = $cat['parent_cid'];
                        array_push($all_cats[$cat['parent_cid']]['value'], $cat['cid']);
                    }

                }


                function make($cats)
                {
                    $i = 0;
                    $edit = array();
                    $del = array();
                    foreach ($cats as $cat) {
                        if ($cat['children_id'] == 0 && $cat['parent_cid'] != 0) {
                            $cats[$cat['parent_cid']]['sub_cats'][$cat['cid']] = $cat;
                            array_push($cats[$cat['parent_cid']]['value'], $cat['cid']);
                            array_push($edit, $cat['parent_cid']);
                            array_push($del, $cat['cid']);
                        }
                        $i++;
                    }


                    $edit = array_unique($edit);

                    foreach ($del as $one) {
                        $ii = 0;
                        foreach ($cats as $cat) {
                            if ($cat['cid'] == $one) {
                                unset($cats[$cat['cid']]);
                            }
                            $ii++;
                        }
                    }

                    //dump($edit);
                    foreach ($edit as $one) {
                        foreach ($cats as $cat) {
                            if ($cat['cid'] == $one) {
                                //dump($cats[$cat['cid']]);
                                $cats[$cat['cid']]['children_id'] = 0;
                                $cats[$cat['cid']]['sub_cats'] = array_values($cats[$cat['cid']]['sub_cats']);
                                $cats[$cat['cid']]['value'] = array_unique($cats[$cat['cid']]['value']);
                            }
                        }
                    }


                    if (count($edit) < 1) {
                        return $cats;
                    } else {
                        return make($cats);
                        //return $cats;
                    }
                }

                //dump(make($all_cats));
                $all_cats = array_values(make($all_cats));

                ccSave($key, $all_cats, 3600);
                $rs_data->getmode = 'SQL';
            }

            $this->ajax($all_cats);

		}else{
			return $this->ajax('',array(1,'未登录',1,'请登录'));
		}
	}

}
